<?php
	require_once("user_session.php");
	if(isset($_GET['id'])){
		$id = $_GET['id'];
		$userId = $_SESSION["userid"];
		$creatorId = EKDocument::getCreatorIdById($id);
		$fatherId = EKDocument::getFatherIdById($id);
		if ($userId == $creatorId
		&& EKDocument::getElementAttributeById($id, "EKDocument", "status") == "pending")
		{
			$title = EKDocument::getTitleById($id);
			$m = EKDocument::deleteDocNode($id);
			echo "<script language=javascript>\n";
			if(substr($m,0,1)=="_")
			{				
				echo "alert(\"Document ". $title ." deleted.\");\n".
					"self.location=\"document.php?id=".$fatherId."&method=v\";\n";
			}else{
				echo "alert(\"".$m."\");\n".
					"self.location=\"document.php?id=".$id."&method=v\";\n";
			}
			echo "</script>";
		} else {
			echo "<script language=javascript>\n".
			        "alert(\"Either the document is not in pending status or you are not the creator of document Id: ". $id .".\");\n".
			        "history.go(-1);\n".
			        "</script>";
		}
	} else {
		echo "<script language=javascript>\n".
			        "history.go(-1);\n".
			        "</script>";
	}
?>